import type { ToPageModel } from '@/models'
import type { RouteLocationNormalizedLoadedGeneric, RouteRecordRaw } from 'vue-router'
import router from '@/router'

/**
 * 页面跳转
 * @param model
 */
export function toPage(model: ToPageModel | string) {
  if (typeof model === 'string') {
    router.push!(model)
    return
  }
  if (model.data?.replace) {
    router.replace!({
      path: model.path,
      query: model.data?.query
    })
    return
  }
  router.push!({
    path: model.path,
    query: model.data?.query
  })
}

/**
 * 返回上一页或指定页面
 * @param path 指定页面地址
 */
export function comeBack(path?: string) {
  if (path) {
    router.replace!(path)
    return
  }
  router.go(-1)
}

/**
 * 生成需要使用的路由数据
 * @param route
 */
export function generateRouteRecordRaw(route: RouteLocationNormalizedLoadedGeneric): RouteRecordRaw {
  return {
    name: route.name,
    path: route.path,
    meta: route.meta
  } as RouteRecordRaw
}
